from . import db
import datetime
from werkzeug.security import generate_password_hash

class User(db.Model):
    __tablename__ = "userinfo"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    username = db.Column(db.String(128), nullable=False)
    _password = db.Column("password", db.String(512), nullable=False)
    role = db.Column(db.Integer, default=0)
    add_time = db.Column(db.DateTime, default=datetime.datetime.now)
    # update_time = db.Column(db.DateTime, default=datetime.datetime.now)

    @property
    def password(self):
        return self._password

    @password.setter
    def password(self,value):
        self._password = generate_password_hash(value)

    @classmethod
    def create_user(cls, username, password):
        user = cls()
        user.username = username
        user.password = password
        db.session.add(user)
        db.session.commit()

